package net.amsoft.iservice.management.webapp.struts.action;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.FileUploadUtil;
import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.struts.form.BannerUploadForm;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

/**
 * Gets called when an ibroker admin opts to change the ad-banner for an 
 * iservice, selects an image or enters a URL pointing to an image and 
 * clicks "Submit" button <br>
 * 
 * @author mukta
 */
public class BannerUploadAction extends Action 
{
    public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		// Process the FormFile
		BannerUploadForm myForm = (BannerUploadForm) form;

		if ((isCancelled(request)) || ("cancel".equals(myForm.getAction())))
		{
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Cancelled Ad Banner Image File Upload..");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to=" + IBrokerConstants.FWD_CANCEL);
			return mapping.findForward(IBrokerConstants.FWD_CANCEL);
		}
		
		// Check if user has come through proper channel or simply typed URL in browser
		FormFile myFile = myForm.getTheFile();
		String link = myForm.getLink();
	    if ((myFile==null) && (link==null))
	    {
            IServiceUtil.log(IServiceConstants.DEBUG, this, "execute(): redirect to=" + 
            		IBrokerConstants.FWD_LOGIN);
	        return mapping.findForward(IBrokerConstants.FWD_LOGIN);
	    }       

		// Check if image or link
		if ((link == null) || link.equals(""))
		{
            IServiceUtil.log(IServiceConstants.INFO, this, 
                    "execute(): Ad Banner Image File -> " + myFile.getFileName());
		    
			//write in file		    
			String serverPath = (String) request.getSession().getAttribute("ServerPath");
			String outfile = serverPath + "staging/" + (String) request.getSession().getAttribute("ServerName") + "_ad-banner.gif";
            ActionMessages oMsgs = new ActionErrors();
			try {
                IServiceUtil.log(IServiceConstants.INFO, this, 
                        "execute(): Calling \"FileUploadUtil.uploadFile()\"");
                FileUploadUtil.uploadFile("Ad-Banner", myFile, outfile);
            } catch (FileNotFoundException e) {
                IServiceUtil.log(IServiceConstants.ERROR, this, 
                        "execute(): Exception=" + e.getMessage());
                oMsgs.add(ActionMessages.GLOBAL_MESSAGE, 
                        new ActionMessage(IBrokerConstants.ERR_FILE_NOT_FOUND));
                saveErrors(request.getSession(), oMsgs);
                IServiceUtil.log(IServiceConstants.DEBUG, this, 
                        "execute(): redirect to="+ mapping.getInputForward());
                return mapping.getInputForward();
            } catch (IOException e) {
                IServiceUtil.log(IServiceConstants.ERROR, this, 
                        "execute(): Exception=" + e.getMessage());
                oMsgs.add(ActionMessages.GLOBAL_MESSAGE, 
                        new ActionMessage(IBrokerConstants.ERR_IO_FAILED));
                saveErrors(request.getSession(), oMsgs);
                IServiceUtil.log(IServiceConstants.DEBUG, this, 
                        "execute(): redirect to="+ mapping.getInputForward());
                return mapping.getInputForward();
            }

            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Setting session attribute for Ad Banner.");
			request.getSession().setAttribute("BannerLink", "staging/" + (String) request.getSession().getAttribute("ServerName") + "_ad-banner.gif");
			
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to="+ IBrokerConstants.FWD_SUCCESS);
			return mapping.findForward(IBrokerConstants.FWD_SUCCESS);
		}
		else
		{
            IServiceUtil.log(IServiceConstants.INFO, this, 
                    "execute(): Ad Banner link -> " + link);
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Setting session attribute for Ad Banner, BanerLink=" + link);
			request.getSession().setAttribute("BannerLink", link);

            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to="+ IBrokerConstants.FWD_BANNER_LINK_SUCCESS);
			return mapping.findForward(IBrokerConstants.FWD_BANNER_LINK_SUCCESS);
		}
		
	}
}